package com.tianxiabuyi.utils;
import com.alibaba.fastjson.TypeReference;
import com.sun.org.apache.xpath.internal.axes.MatchPatternIterator;
import com.tianxiabuyi.Message;
import com.tianxiabuyi.model.pojo.Hospital;
import com.tianxiabuyi.persist.QBuilder;
import com.tianxiabuyi.persist.Where;
import com.tianxiabuyi.weixin.common.media.MediaType;
import com.tianxiabuyi.weixin.common.media.MpArticle;

import com.tianxiabuyi.weixin.common.media.Video;
import com.tianxiabuyi.weixin.common.message.*;
import com.tianxiabuyi.weixin.qy.media.Materials;
import com.tianxiabuyi.weixin.qy.media.Medias;
import com.tianxiabuyi.weixin.qy.message.Messages;
import com.tianxiabuyi.weixin.qy.message.json.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信企业号消息推送工具
 * Created by yanghaixiao on 2017/6/16.
 */
public class WechatQyPushTool {

    /**
     * 根据医院号获取企业管理组账号信息
     * @param hospital
     * @return
     */
    private static Message getCorpidAndSecret(Integer hospital) {
        Message msg = new Message();
        List<Hospital> list = QBuilder.db("basedb").select().from("Hospital").where(new Where("hospital=", hospital).and("status=", ConstantUtil.STATUS_ENABLE)).page(1, 1).query().getResultObj();
        if (Tools.isEmpty(list))
            return new Message(30041, "发送失败,医院不存在");
        Hospital hospitalObj = list.get(0);
        if (Tools.isEmpty(hospitalObj.getJson("corpid")) || Tools.isEmpty(hospitalObj.getJson("secret")))
            return new Message(30041, "发送失败,未设置企业号管理组corpid或secret");
        String corpid = Tools.getDesc(hospitalObj.getJson("corpid"));
        String secret =  Tools.getDesc(hospitalObj.getJson("secret"));
        msg.put("corpid", corpid);
        msg.put("secret", secret);
        return msg;
    }

    /**
     * 推送消息任意类型的消息
     * @param hospital 医院编号
     * @param jsonMessage 消息对象
     * @return
     */
    public static Message push(Integer hospital, JsonMessage jsonMessage) {
        Message qyAccountMsg = getCorpidAndSecret(hospital);
        if (qyAccountMsg.getErrcode() != 0)
            return qyAccountMsg;
        String corpid = Tools.getDesc(qyAccountMsg.get("corpid"));
        String secret =  Tools.getDesc(qyAccountMsg.get("secret"));
        Messages pushTool = Messages.with(corpid, secret);
        return JSONUtil.fromJson(pushTool.send(jsonMessage), Message.class);
    }

    /**
     * 上传永久素材
     * @param hospital
     * @param mediaType
     * @param filePath
     * @return
     */
    public static Message uploadMateria (Integer hospital, MediaType mediaType, String filePath) {
        Message msg = new Message(0, "永久素材上传成功");
        Message qyAccountMsg = getCorpidAndSecret(hospital);
        if (qyAccountMsg.getErrcode() != 0)
            return qyAccountMsg;
        String corpid = Tools.getDesc(qyAccountMsg.get("corpid"));
        String secret =  Tools.getDesc(qyAccountMsg.get("secret"));
        Materials materials = Materials.with(corpid, secret);
        String mediaid = materials.upload(mediaType, filePath);
        msg.put("mediaid", mediaid);
        return msg;
    }

}
